package com.muqi.app.qlearn.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.qlearn.modles.TreeInfo;
import com.muqi.app.qlearn.student.R;

import com.muqi.app.user.widget.SelectableRoundedImageView;

public class TreeAdapter extends BaseAdapter {
	private ArrayList<ArrayList<TreeInfo>> treesInfos;
	private Context content;
	private LayoutInflater inflater;
	private OnTreeLeftClick leftClick;
	private OnTreeRightClick rightClik;

	public TreeAdapter(ArrayList<ArrayList<TreeInfo>> treesInfos,
			Context content) {
		super();
		this.treesInfos = treesInfos;
		this.content = content;
		inflater = LayoutInflater.from(content);
	}

	@Override
	public int getCount() {
		return treesInfos.size();
	}

	@Override
	public Object getItem(int arg0) {
		return treesInfos.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		TreeViewHolder holder = null;
		if (arg1 == null) {
			arg1 = inflater.inflate(R.layout.item_tree, null);
			holder = new TreeViewHolder();
			holder.ll_tree_root = (LinearLayout) arg1
					.findViewById(R.id.ll_root);

			holder.tv_left_score = (TextView) arg1
					.findViewById(R.id.ft_left_score);
			holder.tv_right_score = (TextView) arg1
					.findViewById(R.id.ft_right_score);

			holder.iv_left_type = (ImageView) arg1
					.findViewById(R.id.ft_left_logo);
			holder.iv_right_type = (ImageView) arg1
					.findViewById(R.id.ft_right_logo);

			holder.ll_left_content = (LinearLayout) arg1
					.findViewById(R.id.ft_left_content);
			holder.ll_right_content = (LinearLayout) arg1
					.findViewById(R.id.ft_right_content);

			holder.siv_left_pic = (SelectableRoundedImageView) arg1
					.findViewById(R.id.ft_left_pic);
			holder.siv_right_pic = (SelectableRoundedImageView) arg1
					.findViewById(R.id.ft_right_pic);

			holder.tv_left_time = (TextView) arg1
					.findViewById(R.id.ft_left_time);
			holder.tv_right_time = (TextView) arg1
					.findViewById(R.id.ft_right_time);

			arg1.setTag(holder);
		} else {
			holder = (TreeViewHolder) arg1.getTag();
		}

		if (arg0 == 0) {
			holder.ll_tree_root.setBackgroundResource(R.drawable.tree_top);
		} else if (arg0 == treesInfos.size() - 1) {
			holder.ll_tree_root.setBackgroundResource(R.drawable.tree_buttom);
		} else {
			holder.ll_tree_root.setBackgroundResource(R.drawable.tree_middle);
		}
		// 随机设置logo 回鹘item的最初状态
		holder.iv_left_type.setImageResource(getTreeType("F" + (arg0 + 1)));
		holder.iv_right_type.setImageResource(getTreeType("F" + (arg0 + 2)));
		holder.tv_left_score.setVisibility(View.GONE);
		holder.tv_right_score.setVisibility(View.GONE);
		
		holder.iv_left_type.setVisibility(View.GONE);
		holder.iv_right_type.setVisibility(View.GONE);
		holder.siv_left_pic.setImageDrawable(null);
		holder.siv_right_pic.setImageDrawable(null);
		holder.ll_left_content.setOnClickListener(null);
		holder.ll_right_content.setOnClickListener(null);
		holder.tv_left_time.setText("");
		holder.tv_right_time.setText("");
		
		/*
		 * 设置数据
		 */
		ArrayList<TreeInfo> infos = treesInfos.get(arg0);
		if (infos != null) {
			if (infos.get(0) != null) {
				TreeInfo treeInfo = infos.get(0);
				holder.tv_left_score.setVisibility(View.VISIBLE);
				holder.iv_left_type.setVisibility(View.VISIBLE);
				int resId = getTreeType(treeInfo.getFname());
				if (resId != 0) {
					holder.iv_left_type.setImageResource(resId);
				}
				holder.tv_left_score.setText("+" + treeInfo.getScore());
				holder.tv_left_time.setText(treeInfo.getCreate_time() + "");

				if (treeInfo.picInfo != null
						&& treeInfo.picInfo.fileurl_02 != null) {
					GlideImageUtils.setImageLoader(content,
							treeInfo.picInfo.fileurl_02, holder.siv_left_pic,
							R.color.tree_content_bg_color);
				}
				/**
				 * 设置左右树叶的点击事件
				 */
				holder.ll_left_content
						.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View view) {
								if (leftClick != null) {
									leftClick.onLeftClick(arg0);
								}
							}
						});
			}

			if (infos.get(1) != null) {
				TreeInfo treeInfo = infos.get(1);
				holder.tv_right_score.setVisibility(View.VISIBLE);
				holder.iv_right_type.setVisibility(View.VISIBLE);
				// Log.e("===", treeInfo.getFname()+"right");
				int resId = getTreeType(treeInfo.getFname());
				// Log.e("===", resId+"");
				if (resId != 0) {
					holder.iv_right_type.setImageResource(resId);
				}
				holder.tv_right_score.setText("+" + treeInfo.getScore());
				holder.tv_right_time.setText(treeInfo.getCreate_time() + "");

				if (treeInfo.picInfo != null
						&& treeInfo.picInfo.fileurl_02 != null) {
					GlideImageUtils.setImageLoader(content,
							treeInfo.picInfo.fileurl_02, holder.siv_right_pic,
							R.color.tree_content_bg_color);

				}
				holder.ll_right_content
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						if (rightClik != null) {
							rightClik.onRightClick(arg0);
						}
					}
				});
			}

		}

		return arg1;
	}

	private int getTreeType(String type) {
		int drawableId = 0;
		if (type != null) {
			if ("F1".equals(type)) {
				drawableId = R.drawable.tree_morality;
			} else if ("F2".equals(type)) {
				drawableId = R.drawable.tree_konw;
			} else if ("F3".equals(type)) {
				drawableId = R.drawable.tree_sport;
			} else if ("F4".equals(type)) {
				drawableId = R.drawable.tree_beautiful;
			} else if ("F5".equals(type)) {
				drawableId = R.drawable.tree_labour;
			} else {
				drawableId = R.drawable.tree_morality;
			}
		}
		return drawableId;
	}

	public void setLeftClick(OnTreeLeftClick leftClick) {
		this.leftClick = leftClick;
	}

	public void setRightClik(OnTreeRightClick rightClik) {
		this.rightClik = rightClik;
	}

	private class TreeViewHolder {
		LinearLayout ll_tree_root;
		RelativeLayout rl_left_tree;
		RelativeLayout rl_right_tree;

		TextView tv_left_score;
		ImageView iv_left_type;
		LinearLayout ll_left_content;
		SelectableRoundedImageView siv_left_pic;
		TextView tv_left_time;

		TextView tv_right_score;
		ImageView iv_right_type;
		LinearLayout ll_right_content;
		SelectableRoundedImageView siv_right_pic;
		TextView tv_right_time;
	}

	public interface OnTreeLeftClick {
		void onLeftClick(int position);
	}

	public interface OnTreeRightClick {
		void onRightClick(int position);
	}

}
